package ui.activity.mine;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.MotionEvent;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import net.ck.hzq.R;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import Bean.PresentRecordBean;
import base.BaseAct;
import butterknife.BindView;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.OnLoadMoreListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import cn.finalteam.loadingviewfinal.RecyclerViewFinal;
import event.NetErrorEvent;
import ui.activity.mine.biz.PresentRecordBiz;
import ui.activity.mine.component.DaggerPresentRecordComponent;
import ui.activity.mine.contract.PresentRecordContract;
import ui.activity.mine.module.PresentRecordModule;
import ui.activity.mine.presenter.PresentRcordPresenter;
import ui.adapter.PresentRecordAdapter;
import util.AlertUtils;
import util.SysUtils;


/**
 * 提现列表
 * Created by chenjian on 2017/8/4.
 */

public class PresentRecordAct extends BaseAct implements PresentRecordContract.View {


    @Inject
    PresentRcordPresenter presenter;
    @Inject
    PresentRecordBiz biz;

    @BindView(R.id.profit_day)
    TextView day;
    @BindView(R.id.profit_check)
    TextView check;
    @BindView(R.id.pull_refresh_rv)
    RecyclerViewFinal rv;
    @BindView(R.id.pull_refresh_ptr)
    PtrClassicFrameLayout ptr;

    private SharedPreferences sp;

    PresentRecordAdapter adp;
    String timeStr = "";
    List<PresentRecordBean.ItemsBean> itemsBeans = new ArrayList<>();

    long selectTime;

    @Override
    protected void setRoot() {
//        setTitleBar(R.color.colorBlue);
        setContentView(R.layout.activity_presentrecord);
    }

    @Override
    protected void setup() {
        DaggerPresentRecordComponent.builder().presentRecordModule(new PresentRecordModule(this)).build().inject(this);
        setToolBar(getResources().getString(R.string.myincome3), "");
        presenter.setBiz(biz);
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        selectTime = System.currentTimeMillis();
        try {
            String str = SysUtils.sf2.format(new Date(System.currentTimeMillis())).replace("-", "年")
                    + getResources().getString(R.string.lang_mouth);
            day.setText(str);
            timeStr = SysUtils.sf2.format(new Date(System.currentTimeMillis()));
        } catch (Exception e) {

        }
        sp = getSharedPreferences("ToBuy", Context.MODE_PRIVATE);
        rv.setLayoutManager(new LinearLayoutManager(PresentRecordAct.this));
        rv.setItemAnimator(new DefaultItemAnimator());
//        rv.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        ptr.setLastUpdateTimeRelateObject(this);
        ptr.disableWhenHorizontalMove(true);
        rv.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
            }
        });
        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.getList(true, sp.getString("id", "0"));
            }
        });
        RxView.touches(day)
                .compose(this.<MotionEvent>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(MotionEvent -> {
                    TimePickerDialog timePickerDialog = AlertUtils.timePicker3(PresentRecordAct.this, 365 * 5, 0,selectTime, new OnDateSetListener() {
                        @Override
                        public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                            selectTime=millseconds;
                            String str = SysUtils.sf2.format(new Date(millseconds)).replace("-", "年")
                                    + getResources().getString(R.string.lang_mouth);
                            timeStr = SysUtils.sf2.format(new Date(millseconds));
                            day.setText(str);
                        }
                    });
                    timePickerDialog.show(PresentRecordAct.this.getSupportFragmentManager(), "all");
                });

        RxView.clicks(check)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> setInfo());
        ptr.autoRefresh(true);
    }


    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }

    @Override
    public void upDateUI(List<PresentRecordBean.ItemsBean> bean) {
        itemsBeans.addAll(bean);
        if (adp == null) {
            adp = new PresentRecordAdapter(PresentRecordAct.this, bean);
            rv.setAdapter(adp);
        } else {
            adp.notifyDataSetChanged();
        }
    }

    @Override
    public void upDateSelect(List<PresentRecordBean.ItemsBean> bean) {

    }

    @Override
    public void refreshComplete() {
        ptr.onRefreshComplete();
    }


    @Override
    public void loadMoreComplete() {
        rv.onLoadMoreComplete();
    }

    @Override
    public void hasLoadMore(boolean b) {
        rv.setHasLoadMore(b);
    }


    private void setInfo() {
        if (itemsBeans.size() == 0) {
            showMsg("您暂时没有提现记录");
            return;
        }
        List<PresentRecordBean.ItemsBean> beans = new ArrayList<>();
        for (int i = 0; i < itemsBeans.size(); i++) {
            if (itemsBeans.get(i).getMon().equals(timeStr.replace("-", "."))) {
                beans.add(itemsBeans.get(i));
            }
        }
        if (adp == null) {
            adp = new PresentRecordAdapter(PresentRecordAct.this, beans);
            rv.setAdapter(adp);
        } else {
            adp.getData().clear();
            adp.setDatas(beans);
            adp.notifyDataSetChanged();
        }

    }

}
